package cn.weiyang.spring.mybatis.service.impl;

import cn.weiyang.spring.mybatis.dao.UserMapper;
import cn.weiyang.spring.mybatis.model.UserPage;
import cn.weiyang.spring.mybatis.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 用户服务层实现类
 * Created by Administrator on 2017/7/25.
 */
@Service("userService")
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;
    public UserPage getLikeUsers(Map<String, Object> user) {
        UserPage userPage=new UserPage();
        /*设置每页记录数*/
        int pageSize=Integer.parseInt(String.valueOf(user.get("pageSize")));
        userPage.setPageSize(pageSize);
        /*设置当前页数*/
        int currPage=Integer.parseInt(String.valueOf(user.get("pageNum")));
        userPage.setCurrPage(currPage);
        /*设置总记录数*/
        int totalNum=userMapper.getLikeUsersCount(user);
        userPage.setTotalNum(totalNum);
        //动态计算总页数(总记录数 除以 每页大小，加上  总页数 求余 每页大小，如果余数不为0，则 加 1，否则 加 0 )
        int totalPage=totalNum%pageSize==0?totalNum/pageSize:totalNum/pageSize+1;
        userPage.setTotalPage(totalPage);
        /*设置记录列表*/
        userPage.setResult(userMapper.getLikeUsers(user));
        return userPage;
    }

    @Override
    public int getLikeUsersCount(Map<String, Object> user) {
        return userMapper.getLikeUsersCount(user);
    }
}
